/*
Modifications by: Cary Rivet
Added:
	changed max number of table entries in a directory
*/



// filesys.cc 
//	Routines to manage the overall operation of the file system.
//	Implements routines to map from textual file names to files.
//
//	Each file in the file system has:
//	   A file header, stored in a sector on disk 
//		(the size of the file header data structure is arranged
//		to be precisely the size of 1 disk sector)
//	   A number of data blocks
//	   An entry in the file system directory
//
// 	The file system consists of several data structures:
//	   A bitmap of free disk sectors (cf. bitmap.h)
//	   A directory of file names and file headers
//
//      Both the bitmap and the directory are represented as normal
//	files.  Their file headers are located in specific sectors
//	(sector 0 and sector 1), so that the file system can find them 
//	on bootup.
//
//	The file system assumes that the bitmap and directory files are
//	kept "open" continuously while Nachos is running.
//
//	For those operations (such as Create, Remove) that modify the
//	directory and/or bitmap, if the operation succeeds, the changes
//	are written immediately back to disk (the two files are kept
//	open during all this time).  If the operation fails, and we have
//	modified part of the directory and/or bitmap, we simply discard
//	the changed version, without writing it back to disk.
//
// 	Our implementation at this point has the following restrictions:
//
//	   there is no synchronization for concurrent accesses
//	   files have a fixed size, set when the file is created
//	   files cannot be bigger than about 3KB in size
//	   there is no hierarchical directory structure, and only a limited
//	     number of files can be added to the system
//	   there is no attempt to make the system robust to failures
//	    (if Nachos exits in the middle of an operation that modifies
//	    the file system, it may corrupt the disk)
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#include "copyright.h"

#include "disk.h"
#include "bitmap.h"
#include "directory.h"
#include "filehdr.h"
#include "filesys.h"
#include "system.h"


// Initial file sizes for the bitmap and directory; until the file system
// supports extensible files, the directory size sets the maximum number 
// of files that can be loaded onto the disk.
#define FreeMapFileSize 	(NumSectors / BitsInByte)


// Begin Modifications by Cary Rivet for Project 4
#define NumDirEntries 		8



#define DirectoryFileSize 	(sizeof(DirectoryEntry) * NumDirEntries)

//----------------------------------------------------------------------
// FileSystem::FileSystem
// 	Initialize the file system.  If format = TRUE, the disk has
//	nothing on it, and we need to initialize the disk to contain
//	an empty directory, and a bitmap of free sectors (with almost but
//	not all of the sectors marked as free).  
//
//	If format = FALSE, we just have to open the files
//	representing the bitmap and the directory.
//
//	"format" -- should we initialize the disk?
//----------------------------------------------------------------------

FileSystem::FileSystem(bool format)
{ 
	myDirList = new DirList();
	DEBUG('f', "Initializing the file system.\n");
	
	// setting the initial directory as the root directory
	currentDirSector = DirectorySector;
	
	if (format) {
		BitMap *freeMap = new BitMap(NumSectors);
		Directory *directory = new Directory(NumDirEntries);
		FileHeader *mapHdr = new FileHeader;
		FileHeader *dirHdr = new FileHeader;

		DEBUG('f', "Formatting the file system.\n");

		// First, allocate space for FileHeaders for the directory and bitmap
		// (make sure no one else grabs these!)
		freeMap->Mark(FreeMapSector);	    
		freeMap->Mark(currentDirSector);

		// Second, allocate space for the data blocks containing the contents
		// of the directory and bitmap files.  There better be enough space!

		ASSERT(mapHdr->Allocate(freeMap, FreeMapFileSize));
		ASSERT(dirHdr->Allocate(freeMap, DirectoryFileSize));

		// Flush the bitmap and directory FileHeaders back to disk
		// We need to do this before we can "Open" the file, since open
		// reads the file header off of disk (and currently the disk has garbage
		// on it!).

		DEBUG('f', "Writing headers back to disk.\n");
		mapHdr->WriteBack(FreeMapSector);    
		dirHdr->WriteBack(currentDirSector);

		// OK to open the bitmap and directory files now
		// The file system operations assume these two files are left open
		// while Nachos is running.

		freeMapFile = new OpenFile(FreeMapSector);
		directoryFile = new OpenFile(currentDirSector);
		
		// Once we have the files "open", we can write the initial version
		// of each file back to disk.  The directory at this point is completely
		// empty; but the bitmap has been changed to reflect the fact that
		// sectors on the disk have been allocated for the file headers and
		// to hold the file data for the directory and bitmap.
		directory->setParent(-1);
		DEBUG('f', "Writing bitmap and directory back to disk.\n");
		freeMap->WriteBack(freeMapFile);	 // flush changes to disk
		directory->WriteBack(directoryFile);
		
		if (DebugIsEnabled('f')) {
			freeMap->Print();
			directory->Print();

			delete freeMap; 
			delete directory; 
			delete mapHdr; 
			delete dirHdr;
		}
	} else {
		// if we are not formatting the disk, just open the files representing
		// the bitmap and directory; these are left open while Nachos is running
		freeMapFile = new OpenFile(FreeMapSector);
		directoryFile = new OpenFile(currentDirSector);
		
	}
	rootFile = new OpenFile(currentDirSector);
	rootSector = currentDirSector;	
	
}


void FileSystem::AllowSysCall(){
	//	printf("IN ALLOW\n");
	if(rootFile != NULL && directoryFile != NULL && directoryFile != rootFile){
		
		firstSect = currentDirSector;
		firstFile = directoryFile; 
		
		currentDirSector = rootSector;
		directoryFile = rootFile;
	}
}

void FileSystem::RevertSysCall(){
	if(firstFile != NULL){
		currentDirSector = firstSect;
		directoryFile = firstFile;
	}
}

//----------------------------------------------------------------------
// FileSystem::Create
// 	Create a file in the Nachos file system (similar to UNIX create).
//	Since we can't increase the size of files dynamically, we have
//	to give Create the initial size of the file.
//
//	The steps to create a file are:
//	  Make sure the file doesn't already exist
//        Allocate a sector for the file header
// 	  Allocate space on disk for the data blocks for the file
//	  Add the name to the directory
//	  Store the new file header on disk 
//	  Flush the changes to the bitmap and the directory back to disk
//
//	Return TRUE if everything goes ok, otherwise, return FALSE.
//
// 	Create fails if:
//   		file is already in directory
//	 	no free space for file header
//	 	no free entry for file in directory
//	 	no free space for data blocks for the file 
//
// 	Note that this implementation assumes there is no concurrent access
//	to the file system!
//
//	"name" -- name of file to be created
//	"initialSize" -- size of file to be created
//----------------------------------------------------------------------

bool
FileSystem::Create(char *name, int initialSize)
{
	RevertSysCall();	// LOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOL
	
	
	Directory *directory;
	BitMap *freeMap;
	FileHeader *hdr;
	int sector;
	bool success;

	DEBUG('f', "Creating file %s, size %d\n", name, initialSize);

	directory = new Directory(NumDirEntries);
	directory->FetchFrom(directoryFile);

	if (directory->Find(name) != -1)
	success = FALSE;			// file is already in directory
	else {	
		freeMap = new BitMap(NumSectors);
		freeMap->FetchFrom(freeMapFile);
		sector = freeMap->Find();	// find a sector to hold the file header
		if (sector == -1) 		
		success = FALSE;		// no free block for file header 
		else if (!directory->Add(name, sector, freeMap))		// check this ============================================================================================================================
		success = FALSE;	// no space in directory
		else {
			hdr = new FileHeader;
			if (!hdr->Allocate(freeMap, initialSize))
			success = FALSE;	// no space on disk for data
			else {	
				success = TRUE;
				// everthing worked, flush all changes back to disk
				hdr->WriteBack(sector); 		
				directory->WriteBack(directoryFile);
				freeMap->WriteBack(freeMapFile);
			}
			delete hdr;
		}
		delete freeMap;
	}
	delete directory;
	return success;
}

bool
FileSystem::CreateForDirectory(int size, char *name, int sect){
	RevertSysCall();
	
	OpenFile *tempOpen = new OpenFile(sect);
	
	Directory *directory;
	BitMap *freeMap;
	FileHeader *hdr;
	int sector;
	bool success;

	int initialSize = size;
	
	//DEBUG('f', "Creating file %s, size %d\n", name, initialSize);

	directory = new Directory(NumDirEntries);
	directory->FetchFrom(tempOpen);

	if (directory->Find(name) != -1)
	success = FALSE;			// file is already in directory
	else {	
		freeMap = new BitMap(NumSectors);
		freeMap->FetchFrom(freeMapFile);
		sector = freeMap->Find();	// find a sector to hold the file header
		if (sector == -1) 		
		success = FALSE;		// no free block for file header 
		else if (!directory->Add(name, sector, freeMap))		// check this ============================================================================================================================
		success = FALSE;	// no space in directory
		else {
			hdr = new FileHeader;
			if (!hdr->Allocate(freeMap, initialSize))
			success = FALSE;	// no space on disk for data
			else {	
				success = TRUE;
				// everthing worked, flush all changes back to disk
				hdr->WriteBack(sector); 		
				directory->WriteBack(directoryFile);
				freeMap->WriteBack(freeMapFile);
			}
			delete hdr;
		}
		delete freeMap;
	}
	delete directory;
	return success;
}

bool
FileSystem::CreateDir(char *name){
	RevertSysCall();
	Directory *directory;
	BitMap *freeMap;
	FileHeader *hdr;
	OpenFile *myOF;
	int sector;
	bool success;

	DEBUG('f', "Creating directory %s, \n", name);

	if (directoryFile != NULL){
		delete directoryFile;
	}
	directoryFile = new OpenFile(currentDirSector);
	
	directory = new Directory(NumDirEntries);
	directory->FetchFrom(directoryFile);

//	printf("Current Directory Sector In CreateDir %d\n", currentDirSector);
	
	
	
	if (directory->Find(name) != -1)
	success = FALSE;			// file is already in directory
	else {	
		freeMap = new BitMap(NumSectors);
		freeMap->FetchFrom(freeMapFile);
		sector = freeMap->Find();	// find a sector to hold the file header
	//	printf("Sector: %d \n", sector);
		if (sector == -1) 		
		success = FALSE;		// no free block for file header 
		else if (!directory->AddDir(name, sector, freeMap))	{	// check this ============================================================================================================================
			//printf("Did False in create DIR\n");
			success = FALSE;	// no space in directory
		}
		else {
			hdr = new FileHeader;

			if (!hdr->Allocate(freeMap, DirectoryFileSize)){ 
				success = FALSE;
			}else{
				
				directory->WriteBack(directoryFile);
				delete directory;
				directory = new Directory(NumDirEntries);
			
				directory->setParent(currentDirSector);
				hdr->WriteBack(sector);

			//	printf("Directorys parent is : %d \n", directory->getParent());
				
				myOF = new OpenFile(sector);
								
				//directory->AddDir(name, sector, freeMap);
			//	printf("Making dir\n");
				directory->WriteBack(myOF);
				
				success = TRUE;
				// everthing worked, flush all changes back to disk
				//hdr->Print();
				//printf("Directory print: \n");
				//directory->Print();
				
				freeMap->WriteBack(freeMapFile);
				//printf("NAME OF NEW DIR %s. \n", name);
				//printf("The Name of Parent Dir %s. \n", myDirList->getName(currentDirSector));
				myDirList->addDir(name, myDirList->getName(currentDirSector), currentDirSector, sector);
				//hdr->FetchFrom(myOF);
			//	printf("NEW DIRECTS LS\n");
				//hdr->Print();
				
				delete myOF;
			}
			
				delete hdr;
		}
		delete freeMap;
	}
	delete directory;
	
	directory = new Directory(NumDirEntries);
	myOF = new OpenFile(sector);
	
	directory->FetchFrom(myOF);
	directory->List();
	
	delete directory;
	delete myOF;
	
	
	
	
	
	
	
	return success;
}

bool
FileSystem::RemoveDir(char *name)
{ 
	RevertSysCall();
	Directory *directory;
	BitMap *freeMap;
	Directory *fileHdr;
	OpenFile *copyDir;
	OpenFile *newOF;
	int sector;
	int copySector;
	int dirCount = 0;
	int fileCount = 0;
	dirNode **childDir;
	int childCount = 0;
	int childDirCount = 0;
	
	directory = new Directory(NumDirEntries);
	directory->FetchFrom(directoryFile);
	sector = directory->Find(name);
	if (sector == -1) {
	//	printf("neg one case\n");
		delete directory;
		return FALSE;			 // file not found 
	}
	newOF = new OpenFile(sector);
	fileHdr = new Directory(NumDirEntries);
	fileHdr->FetchFrom(newOF);
	
	
	for(dirNode *i = myDirList->getFirst(); i != NULL; i= i->next){
		if(i->parentSector == sector){
			dirCount++;
		}
	}

	childDir = new dirNode*[dirCount];
	
	for(dirNode *i = myDirList->getFirst(); i != NULL; i = i->next){	// for loop to find the children directs inside this direct
		if(i->parentSector == sector){			
			childDir[childDirCount++] = i;
		}
	}
	
	copySector = currentDirSector;
	copyDir = new OpenFile(copySector);
	
	for(int i = 0; i < dirCount; i++){
		currentDirSector = childDir[i]->sector;
		directoryFile = new OpenFile(currentDirSector);
		
	//	printf("child dir %d name %s \n", i, childDir[i]->name);
		
		fileSystem->RemoveDir(childDir[i]->name);
		currentDirSector = copySector;
		directoryFile = copyDir;
		
	}
	
	for(int i = 0; i < dirCount; i++){
		fileHdr->clearDirFiles(freeMap);
	}
	
	
	freeMap = new BitMap(NumSectors);
	freeMap->FetchFrom(freeMapFile);

	freeMap->Clear(sector);			// remove header block
	directory->Remove(name, freeMap);
	
	freeMap->WriteBack(freeMapFile);		// flush to disk
	directory->WriteBack(directoryFile);        // flush to disk
	
	delete childDir;

	delete copyDir;
	
	delete fileHdr;
	delete directory;
	delete freeMap;
	return TRUE;
} 


bool
FileSystem::CreateDirByPath(char *name, char *path){

}

//----------------------------------------------------------------------
// FileSystem::Open
// 	Open a file for reading and writing.  
//	To open a file:
//	  Find the location of the file's header, using the directory 
//	  Bring the header into memory
//
//	"name" -- the text name of the file to be opened
//----------------------------------------------------------------------

OpenFile *
FileSystem::Open(char *name)
{ 
	
	Directory *directory = new Directory(NumDirEntries);
	OpenFile *openFile = NULL;
	int sector;

	DEBUG('f', "Opening file %s\n", name);
	directory->FetchFrom(directoryFile);
	sector = directory->Find(name); 
	if (sector >= 0) 		
	openFile = new OpenFile(sector);	// name was found in directory 
	delete directory;
	return openFile;				// return NULL if not found
}

OpenFile *
FileSystem::getOpenFileBySect(char* name, int sector){
	RevertSysCall();
	Directory *directory = new Directory(NumDirEntries);
	OpenFile *tempFile = new OpenFile(sector);
	OpenFile *openFile = NULL;

	DEBUG('f', "Opening file %s\n", name);
	directory->FetchFrom(tempFile);
	sector = directory->Find(name); 
	if (sector >= 0) 		
	openFile = new OpenFile(sector);	// name was found in directory 
	delete directory;
	return openFile;				// return NULL if not found
}

//----------------------------------------------------------------------
// FileSystem::Remove
// 	Delete a file from the file system.  This requires:
//	    Remove it from the directory
//	    Delete the space for its header
//	    Delete the space for its data blocks
//	    Write changes to directory, bitmap back to disk
//
//	Return TRUE if the file was deleted, FALSE if the file wasn't
//	in the file system.
//
//	"name" -- the text name of the file to be removed
//----------------------------------------------------------------------

bool
FileSystem::Remove(char *name)
{ 
	RevertSysCall();
	Directory *directory;
	BitMap *freeMap;
	FileHeader *fileHdr;
	int sector;
	
	directory = new Directory(NumDirEntries);
	directory->FetchFrom(directoryFile);
	sector = directory->Find(name);
	if (sector == -1) {
		delete directory;
		return FALSE;			 // file not found 
	}
	fileHdr = new FileHeader;
	fileHdr->FetchFrom(sector);

	freeMap = new BitMap(NumSectors);
	freeMap->FetchFrom(freeMapFile);

	fileHdr->Deallocate(freeMap);  		// remove data blocks
	freeMap->Clear(sector);			// remove header block
	directory->Remove(name, freeMap);
	
	freeMap->WriteBack(freeMapFile);		// flush to disk
	directory->WriteBack(directoryFile);        // flush to disk
	delete fileHdr;
	delete directory;
	delete freeMap;
	return TRUE;
} 

//----------------------------------------------------------------------
// FileSystem::List
// 	List all the files in the file system directory.
//----------------------------------------------------------------------

void
FileSystem::List()
{
	RevertSysCall();
	Directory *directory = new Directory(NumDirEntries);

	directory->FetchFrom(directoryFile);
	directory->List();
	directory->Print();
	delete directory;
}

//----------------------------------------------------------------------
// FileSystem::Print
// 	Print everything about the file system:
//	  the contents of the bitmap
//	  the contents of the directory
//	  for each file in the directory,
//	      the contents of the file header
//	      the data in the file
//----------------------------------------------------------------------

void
FileSystem::Print()
{
	RevertSysCall();
	FileHeader *bitHdr = new FileHeader;
	FileHeader *dirHdr = new FileHeader;
	BitMap *freeMap = new BitMap(NumSectors);
	Directory *directory = new Directory(NumDirEntries);

	printf("Bit map file header:\n");
	bitHdr->FetchFrom(FreeMapSector);
	bitHdr->Print();

	printf("Directory file header:\n");
	dirHdr->FetchFrom(currentDirSector);
	dirHdr->Print();

	freeMap->FetchFrom(freeMapFile);
	freeMap->Print();

	directory->FetchFrom(directoryFile);
	directory->Print();

	delete bitHdr;
	delete dirHdr;
	delete freeMap;
	delete directory;
} 


void
FileSystem::PWD_PRIME(){
	RevertSysCall();
	char *path = new char[300];
	path[0] = '\0';
	char *tempString = new char[300];
	tempString[0] = '\0';
	int tempSect = 0;
	OpenFile *tempFile;
	int childSect = currentDirSector;
	Directory *tempDir = new Directory(NumDirEntries);
	tempDir->FetchFrom(directoryFile);
	
	
	if(currentDirSector != rootSector){
		while(tempDir->getParent() != -1){
			tempSect = tempDir->getParent();
			tempFile = new OpenFile(tempSect);
			tempDir->FetchFrom(tempFile);
			sprintf(tempString, tempDir->getChildNameBySector(childSect));
			strcat(tempString, "/");
			strcat(tempString, path);
			sprintf(path, tempString);
			childSect = tempSect;
		}
		printf("/%s", path);
	}
	else{
		printf("/\n");
	}
	
}

void
FileSystem::SYS_LS(){
	RevertSysCall();
//	printf("The Sector is: %d \n", currentDirSector);
	directoryFile = new OpenFile(currentDirSector);
	/*	for(dirNode *i = myDirList->getFirst(); i != NULL; i= i->next){
		printf("Name %s \n", i->name);
		printf("Sector %d \n", i->sector);
	}*/
	Directory *directory = new Directory(NumDirEntries);
	directory->FetchFrom(directoryFile);
	directory->PrintForLS();
//	printf("Directs Parent: %d \n", directory->getParent());
	delete directory;
}

void 
FileSystem::SYS_LS_L(){
	RevertSysCall();
	//	printf("INSIDE FILESYS SYS_LS_L\n");
	
	Directory *directory = new Directory(NumDirEntries);
	directory->FetchFrom(directoryFile);
	directory->PrintForLSL();
	delete directory;
}

bool
FileSystem::changeDir(int chgDir){
	RevertSysCall();

	Directory *tempDir = new Directory(NumDirEntries);
	currentDirSector = chgDir;
	
	directoryFile = new OpenFile(chgDir);
		
//	printf("CurrentDirSector %d \n");
	
	delete tempDir;
	
	
	return true;
}

bool
FileSystem::changeDirNoPath(char *path){
	RevertSysCall();
//	printf("Change Dir No Path\n");

	char myBuff;
	char dirName[20];
	char *tempPath;
	int tempDirSect = -1;
	Directory *tempDir = new Directory(NumDirEntries);
	OpenFile *myOF;
	bool found = false;
	int i = 0;
	
	
	
	
//	printf("CurrentDirSect: %d \n", currentDirSector);
	tempDirSect = currentDirSector;
	myOF = new OpenFile(tempDirSect);
//	printf("Did the openFile\n");
	tempDir->FetchFrom(directoryFile);
//	printf("FetchFrom\n");
	
	int nameNum = 0;
	
	while(path[i] != '\0'){
		dirName[i] = path[i];
		i++;
	}

//	printf("Path: %s.  \n", path);
//	printf("dirName: %s \n", dirName);

	if(strcmp(path, "..") == 0){
//		printf("In IF\n");
		tempDirSect = myDirList->getParentSector(tempDirSect);
		if(tempDirSect == NULL){
			return false;
		}
		myOF = new OpenFile(tempDirSect);
		tempDir->FetchFrom(myOF);
		if(tempDirSect < 0){
				changeToRoot();
			}
	}
	else{
//		printf("In Else\n");
	/*	for(dirNode *i = myDirList->getFirst(); i != NULL; i = i->next){
			printf("Directory %d, Directory Name %s \n", i, i->name);
			if(strcmp(i->name,path) == 0){
				printf("Name is dirName\n");
				tempDirSect = i->sector;
				myOF = new OpenFile(tempDirSect);
				tempDir->FetchFrom(myOF);
				found = true;
				break;
			}
		}*/
		
		tempDirSect = tempDir->Find(path);
		
	//	printf("i = %i\n", tempDirSect);
		if (tempDirSect > 0)
			found = true;
		
		if(found == false){				// if there was nothing in the list with that name
		//	printf("Nothing was found in the list.\n");
			return false;
		}
	}

	//printf("TempDirSector: %d \n", tempDirSect);
	currentDirSector = tempDirSect;
	directoryFile = new OpenFile(currentDirSector);
	//tempDir->WriteBack(directoryFile);
	//	directoryFile->WriteBack();
	
	
	return true;
}


int FileSystem::getCurrDirSector(){
	RevertSysCall();
	return currentDirSector;
}


int FileSystem::parsePath(char* name){
	//printf("Doing parse path\n");
	Directory *tempDir = new Directory(NumDirEntries);
	tempDir->FetchFrom(directoryFile);
	int tempDirSect = currentDirSector;
	OpenFile* myOF;
	bool found = false;
	
	char *remainder = new char[strlen(name)];
	
	
	int x = 0;
	int specCheck = 0;
	
	if(name[0] == '/'){
	//	printf("name b4 parse: %s \n", name);
		tempDir->FetchFrom(rootFile);
		tempDirSect = rootSector;
		x++;
	//	printf("name b4 parse: %s \n", name);
		sprintf(remainder, name+x);
	//	printf("Remainder %s \n", remainder);
	//	printf("After Sprintf\n");
		while(strlen(remainder) > 0){
//		printf("Remainder in While %s \n", remainder);
//		printf("B4 myBuff init\n");
		char *myBuff = new char[FileNameMaxLen+1];
//		printf("After buff init\n");
//		printf("Length of remainder %d \n", strlen(remainder));
			for(int i = 0; remainder[i] != NULL && i < (strlen(remainder)+1); i++){
				specCheck = i;
				if(remainder[i] != '/' && remainder[i] != NULL && remainder[i] != '\0' && remainder[i] != '\n'){
					myBuff[i] = remainder[i];
					x++;
				//	printf("The i is %d ", i);
				//	printf("My Buff %s \n", myBuff);
				}
				else{
					x++;
					myBuff[i] = '\0';
				//	printf("The buffer is %s \n", myBuff);
					break;
				}
			}
			//printf("Out of for loop \n");
			
			myBuff[specCheck+1] = '\0';
			
		//	printf("My Buffer %s \n", myBuff);
			
			if((strcmp(myBuff, "..") != 0) && (strcmp(myBuff, ".") != 0)){
				tempDirSect = tempDir->Find(myBuff);
				if(tempDirSect >= 0){
					myOF = new OpenFile(tempDirSect);
					tempDir->FetchFrom(myOF);
				}
			}
			
			if(strcmp(myBuff, "..") == 0){
				tempDirSect = myDirList->getParentSector(tempDirSect);
				if(tempDirSect > 0){
					myOF = new OpenFile(tempDirSect);
					tempDir->FetchFrom(myOF);
				}
			}
			
			if(tempDirSect < 0){
				changeToRoot();
				return NULL;
			}
			sprintf(remainder, name+x);
		}		// while there is stuff left in the name
		
		myOF = new OpenFile(tempDirSect);
		
		
		//printf("*****Sector of parsed file: %d", tempDirSect);
		
		return tempDirSect;
	}
	else{
	//	printf("name b4 parse: %s \n", name);
		sprintf(remainder, name+x);
	//	printf("Remainder %s \n", remainder);
	//	printf("After Sprintf\n");
		while(strlen(remainder) > 0){
	//	printf("Remainder in While %s \n", remainder);
	//	printf("B4 myBuff init\n");
		char *myBuff = new char[FileNameMaxLen+1];
	//	printf("After buff init\n");
	//	printf("Length of remainder %d \n", strlen(remainder));
			for(int i = 0; remainder[i] != NULL && i < (strlen(remainder)+1); i++){
				specCheck = i;
				if(remainder[i] != '/' && remainder[i] != NULL && remainder[i] != '\0' && remainder[i] != '\n'){
					myBuff[i] = remainder[i];
					x++;
		//			printf("The i is %d ", i);
		//			printf("My Buff %s \n", myBuff);
				}
				else{
					x++;
					myBuff[i] = '\0';
		//			printf("The buffer is %s \n", myBuff);
					break;
				}
			}
		//	printf("Out of for loop \n");
			
			myBuff[specCheck+1] = '\0';
			
		//	printf("My Buffer %s \n", myBuff);
			
			if((strcmp(myBuff, "..") != 0) && (strcmp(myBuff, ".") != 0)){
				tempDirSect = tempDir->Find(myBuff);
				if(tempDirSect > 0){
					myOF = new OpenFile(tempDirSect);
					tempDir->FetchFrom(myOF);
				}
			}
			
			if(strcmp(myBuff, "..") == 0){
				tempDirSect = myDirList->getParentSector(tempDirSect);
				if(tempDirSect > 0){
					myOF = new OpenFile(tempDirSect);
					tempDir->FetchFrom(myOF);
				}
			}
			
			if(tempDirSect < 0){
				changeToRoot();
				return NULL;
			}
			sprintf(remainder, name+x);
		}		// while there is stuff left in the name
		
		myOF = new OpenFile(tempDirSect);
		
		
	//	printf("*****Sector of parsed file: %d", tempDirSect);
		
		return tempDirSect;
	}
}

void
FileSystem::changeToRoot(){
		currentDirSector = rootSector;
		directoryFile = rootFile;
}

void
FileSystem::removeFromDir(char *name, int sect){
	RevertSysCall();
	Directory *directory;
	BitMap *freeMap;
	OpenFile *tempFile = new OpenFile(sect);
	
	FileHeader *fileHdr;
	int sector;
	
	directory = new Directory(NumDirEntries);
	directory->FetchFrom(tempFile);
	
	sector = directory->Find(name);
	if (sector == -1) {
		delete directory;
	}
	fileHdr = new FileHeader;
	fileHdr->FetchFrom(sector);

	freeMap = new BitMap(NumSectors);
	freeMap->FetchFrom(freeMapFile);

	fileHdr->Deallocate(freeMap);  		// remove data blocks
	freeMap->Clear(sector);			// remove header block
	directory->Remove(name, freeMap);
	
	freeMap->WriteBack(freeMapFile);		// flush to disk
	directory->WriteBack(directoryFile);        // flush to disk
	delete fileHdr;
	delete directory;
	delete freeMap;


}










